import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:management/model/user_model.dart';

///更改密码
class ChangePassword extends StatefulWidget {
  @override
  _ChangePasswordState createState() => _ChangePasswordState();
}

class _ChangePasswordState extends State<ChangePassword> {
  GlobalKey<FormState> _formKey = GlobalKey();
  TextEditingController _oldPswController = TextEditingController();
  TextEditingController _newPswController = TextEditingController();
  TextEditingController _newPswAgainController = TextEditingController();

  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIOverlays([]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () => Navigator.of(context).pop(),
          iconSize: 35,
        ),
        title: Text(
          "修改密码",
          style: TextStyle(
            fontSize: 25,
          ),
        ),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Container(
          height: 310,
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                Container(
                  height: 100,
                  child: TextFormField(
                    controller: _oldPswController,
                    style: TextStyle(fontSize: 30),
                    decoration: InputDecoration(
                        prefixIcon: Icon(Icons.lock), hintText: "请输入旧密码"),
                    obscureText: true,
                    validator: _validatorOldPsw,
                  ),
                ),
                Container(
                  height: 80,
                  child: TextFormField(
                    controller: _newPswController,
                    style: TextStyle(
                      fontSize: 30,
                    ),
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.lock),
                      hintText: "请输入新密码",
                    ),
                    obscureText: true,
                    validator: _validatorNewPsw,
                  ),
                ),
                Container(
                  height: 80,
                  child: TextFormField(
                    controller: _newPswAgainController,
                    style: TextStyle(
                      fontSize: 30,
                    ),
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.lock),
                      hintText: "确认新密码",
                    ),
                    obscureText: true,
                    validator: _validatorNewPswAgain,
                  ),
                ),
                Expanded(
                  child: MaterialButton(
                    color: Colors.blue,
                    minWidth: 200,
                    height: 35,
                    child: Text(
                      "确认",
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                    onPressed: _onConfirm,
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  String? _validatorOldPsw(String? value) {
    if (value!.isEmpty) {
      return "密码不能为空";
    }
    return null;
  }

  String? _validatorNewPsw(String? value) {
    if (value!.isEmpty) {
      return "密码不能为空";
    }

    return null;
  }

  String? _validatorNewPswAgain(String? value) {
    if (value!.isEmpty) {
      return "密码不能为空";
    }
    if (value != _newPswController.text) {
      return "密码不一致";
    }
    return null;
  }

  void _onConfirm() async {
    _formKey.currentState!.save();
    if (_formKey.currentState!.validate()) {
      User? user = await UserProvider.instance.queryName("admin");
      String oldPsw = _oldPswController.text;
      if (user!.psw != oldPsw) {
        Fluttertoast.showToast(msg: "旧密码错误");
        return;
      }
      String newPsw = _newPswController.text;
      await UserProvider.instance
          .update(User("admin", oldPsw), User("admin", newPsw));
      Navigator.of(context).popUntil(ModalRoute.withName('/LoginPage'));
      Navigator.of(context).pushNamed("/LoginPage");
    }
  }
}
